package com.bocai.kelalacar.ui.car.main.act;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.kelalacar.App;
import com.bocai.kelalacar.R;
import com.bocai.kelalacar.base.BaseActivity;
import com.bocai.kelalacar.bean.MessageBean;
import com.bocai.kelalacar.ui.car.main.ada.MessageAda;
import com.bocai.kelalacar.ui.car.main.contract.MessageContract;
import com.bocai.kelalacar.ui.car.main.module.MessageModule;
import com.bocai.kelalacar.ui.car.main.presenter.MessagePresenter;
import com.bocai.kelalacar.ui.car.order.act.MyCarOrderActivity;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by yuanfei on 2017/7/8.
 */

public class MessageActivity extends BaseActivity implements MessageContract.view,XRecyclerView.LoadingListener{
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.ll_delete)
    LinearLayout llDelete;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.xrecycle)
    XRecyclerView xrecycle;
    @Inject
    MessagePresenter presenter;
    MessageAda messageAda;
    private int pageIndex = 1;
    @BindView(R.id.ll_back)
    LinearLayout llBack;

    @Override
    public int getLayoutId() {
        return R.layout.act_message;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new MessageModule(this)).inject(this);
    }

    @Override
    public void initView() {
        initToolbar("消息");
        initBack(imgBack,llBack);
        messageAda = new MessageAda(this,presenter);
        xrecycle.setLayoutManager(new LinearLayoutManager(this));
        xrecycle.setAdapter(messageAda);
        xrecycle.setLoadingListener(this);
        xrecycle.refresh();
        messageAda.setOnItemClickedListener(new MessageAda.OnItemClickedListener() {
            @Override
            public void onItemClicked(int index) {
                if (messageAda.getDataList().get(index).getCategory() == 3){
//                    Intent intent = new Intent(MessageActivity.this,ModifyAddressActivity.class);
//                    intent.putExtra("orderId",messageAda.getDataList().get(index).getSourceId());
//                    if (messageAda.getDataList().get(index).getTitle().equals("行程线路确认修改")){
//                        intent.putExtra("index",1);
//                    }else if (messageAda.getDataList().get(index).getTitle().equals("行程线路拒绝修改")){
//                        intent.putExtra("index",1);
//                    }
//                    startActivityForResult(intent,123);
                    startActivity(MyCarOrderActivity.class);
                }
            }
        });
    }

    @Override
    public void getMessageList(MessageBean bean) {
        xrecycle.refreshComplete();
        xrecycle.loadMoreComplete();
        if (bean != null && bean.getMessages() != null) {
            if (pageIndex == 1) {
                messageAda.clear();
                messageAda.addItems(bean.getMessages());
            } else {
                messageAda.addItems(bean.getMessages());
            }
            if (bean.getMessages().size() > 10) {
                xrecycle.setLoadingMoreEnabled(false);
            } else {
                xrecycle.setLoadingMoreEnabled(true);
            }
        }else {
            xrecycle.setLoadingMoreEnabled(false);
        }
    }

    @Override
    public void readMessage() {

    }

    @Override
    public void deleteMessage(int position) {
        showErrorTip("删除成功");
        messageAda.getDataList().remove(position);
        messageAda.notifyDataSetChanged();
    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        showShortToast(msg);
        if (msg.equals("您的登录已失效，请重新登录")){
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }else if (msg.equals("网络异常")){
            xrecycle.refreshComplete();
            xrecycle.loadMoreComplete();
        }
    }

    @Override
    public void onRefresh() {
        pageIndex = 1;
        presenter.messageList(pageIndex+"","10");
    }

    @Override
    public void onLoadMore() {
        pageIndex = pageIndex + 1;
        presenter.messageList(pageIndex+"","10");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 123&&resultCode == 123){
            xrecycle.refresh();
        }
    }
}
